<?php
declare(strict_types=1);

namespace WebApp\lib\app;

use Exception;
use Swoole\Http\Response;

/**
 * Class Resp
 * @package WebApp\lib\app
 */
class RespProxy
{
    /**
     * @var Response
     */
    private $response;

    /**
     * Resp constructor.
     * @param $response
     */
    public function __construct(&$response)
    {
        $this->response = $response;
    }

    public function __destruct()
    {
        unset($this->response);
    }

    /**
     * @param string $str
     */
    public function write($str = '')
    {
        if ($str) {
            if(ENV_PHP_FPM){
                echo $str;
            } else {
                $this->response->write($str);
            }
        }
    }

    /**
     * @param string $str
     * @return string|bool
     * @throws Exception
     */
    public function end($str = '')
    {
        $this->write($str);
        throw new Exception('111', 111);
    }

    public function header(string $key, string $val) {
        if(ENV_PHP_FPM){
            header("$key: $val");
        } else {
            $this->response->header($key, $val);
        }
    }
}